<template>
  <div class="app-container customer-div">
      <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="my">我的</el-menu-item>
      <el-menu-item index="all" v-if="purchaseAfterSettlementForAll">全部</el-menu-item>
    </el-menu>
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
        <el-form-item label="采购订单号：">
           <el-input v-model="listQuery.orderSn" clearable></el-input>
        </el-form-item>
        <el-form-item label="供应商：">
          <el-select
                  clearable
                  v-model="listQuery.supplierId"
                  filterable
                  reserve-keyword
                  placeholder="请输入关键词"
                  style="width: 220px;">
                  <el-option
                          v-for="item in options2"
                          :key="item.supplierId"
                          :label="item.supplierName"
                          :value="item.supplierId">
                  </el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="采购员：" >
              <el-select v-model="listQuery.userId" filterable placeholder="请选择" clearable style="width:150px">
                  <el-option
                          v-for="item in userOptionList"
                          :key="item.userId"
                          :label="item.userName"
                          :value="item.userId">
                  </el-option>
              </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="handleSearchList()"
              type="primary">
              查询
            </el-button>
          </el-form-item>
      </el-form>
    </div>

      <!--数据列表-->
      <div>
        <el-radio-group v-model="listQuery.applyState" size="small" @change="handleSearchList()">
          <el-radio-button label="">全部</el-radio-button>
          <el-radio-button label="0">审核中</el-radio-button>
          <el-radio-button label="10">已审核</el-radio-button>
          <el-radio-button label="20">已拒绝</el-radio-button>
        </el-radio-group>
      </div>
    <div class="table-container">
        <el-table
          :header-cell-style="{'text-align':'center'}"
          v-loading="listLoading"
          :data="tableData"
          border
          style="width: 100%; margin-top: 20px">
            <el-table-column
                    align="center"
                    label="供应商名称"
                    min-width="200">
                <template slot-scope="scope">
                    {{scope.row.supplierName}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="采购单号"
                   min-width="150">
                <template slot-scope="scope">
                  {{scope.row.orderSn}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="采购员"
                    min-width="100">
                <template slot-scope="scope">
                  {{scope.row.userName}}
                </template>
            </el-table-column>
            <el-table-column
                    label="商品信息"
                    min-width="200"
                    align="center">
                <template slot-scope="scope">
                  {{scope.row.goodsSn}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="合同总价"
                    min-width="100">
                <template slot-scope="scope">
                  {{scope.row.goodsAmount}}
                </template>
            </el-table-column>
            <el-table-column
                    align="center"
                    label="合同数量"
                    min-width="100">
                <template slot-scope="scope">
                  {{scope.row.goodsNum}}
                </template>
            </el-table-column>
          <el-table-column  label="合同单价" min-width="150" align="center">
            <template slot-scope="scope">
              {{scope.row.goodsPrice}}
            </template>
          </el-table-column>

          <el-table-column
                  label="结算单价"
                  min-width="100"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.settlePrice}}
            </template>
          </el-table-column>
          <el-table-column
                  label="审核状态"
                  min-width="100"
                  align="center">
            <template slot-scope="scope">
              {{scope.row.applyState | fifapplyState}}
            </template>
          </el-table-column>

        </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>

  </div>
</template>
<script>
  import {getOrderUsersList,supplierList} from '@/api/partner'
  import {settleApplyList} from '@/api/company'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  import {validateCommission} from '@/utils/validate';

  const defaultListQuery={
    "orderSn" : "", // 订单号
    "supplierId" : '', //供应商ID
    "userId" : '', //采购员ID
    "applyState" : '',// 申请状态(0审核中，10已通过，20已拒绝)
    tab:'my',
    "pageNum" : 1,
    "pageSize" : 10
  };

  export default {
    name: "purchaseAfterSettlement",
    data(){

      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        count: 0,
        listLoading: false,
        userOptionList:[],
        roles:store.getters.roles ? store.getters.roles : [],
        options2:[]
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId',
        'isMange',
      ]),
      purchaseAfterSettlementForAll (){
            if(this.roles.indexOf("purchaseAfterSettlementForAll") != -1){
                return true
            }else{
                return false
            }
        },
    },
    created() {
      this.getList();
      this.getUserOptionList();
      this.getSupplierList()
    },
    filters: {
      fifapplyState(val){
        if(val == 0){
          return '审核中'
        }else if(val == 10){
          return '已通过'
        }else if(val == 20){
          return '已拒绝'
        }
      }

    },
    methods:{

      //供应商
      getSupplierList() {
          supplierList().then((res)=>{
            this.options2 = res;
          })
      },

      //获取员工下拉选项
      getUserOptionList(){
        getOrderUsersList({departmentId:3,status:1}).then((res)=>{
          this.userOptionList = res;
        })
      },


      /**
       * 切换选择状态
       */
      handleSelect(key, keyPath){
        this.listQuery = JSON.parse(JSON.stringify(defaultListQuery))
        this.listQuery.tab = key;
        this.handleSearchList();
      },
      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
      },

      //获取订单列表
      getList() {
        this.listLoading = true;
        settleApplyList(this.listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },

    },
  }
</script>

<style scoped lang="scss">

</style>
